package com.ymatou.iapi.dglogin.testcase;

import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.dglogin.parameter.AppSourceEnum;
import com.ymatou.iapi.dglogin.parameter.UsernameandpwdloginBean;
import com.ymatou.iapi.dglogin.service.UsernameandpwdloginCall;
import com.ymatou.iapi.dguser.parameter.CreateUserBackBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.mchCreateCsBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.cs_info;
import com.ymttest.database.mongo.UserTokenWapper;
import com.ymttest.database.sqlwapper.DGUserWapper;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
import com.ymttest.utils.TestDataManager;
/**
*用户名密码登录
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_Usernameandpwdlogin{
	private static UsernameandpwdloginBean usernameandpwdloginBean;
	private static UsernameandpwdloginCall usernameandpwdloginCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("用户名密码登录");
	}

	@Before
	public void caseUp() {
		usernameandpwdloginBean=new UsernameandpwdloginBean();
		usernameandpwdloginCall=new UsernameandpwdloginCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(usernameandpwdloginCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_001() {
		Logger.start(true,"商家用户名密码登录成功");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setIsSetPwd(true);
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_002() {
		Logger.start(true,"密码错误失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd()+1);
			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals("105", usernameandpwdloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_003() {
		Logger.start(true,"不输用户名失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd()+1);
//			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", usernameandpwdloginCall.getString("errorCode"), "验证errorCode");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_004() {
		Logger.start(true,"不输密码失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd()+1);
//			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", usernameandpwdloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_005() {
		Logger.start(true,"普通用户密码登录成功");
		try {
			UserCreateBean u = DGUserCallService.initUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_006() {
		Logger.start(true,"不输appSource失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
//			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals("1", usernameandpwdloginCall.getString("errorCode"), "验证errorCode");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_007() {
		Logger.start(true,"商家手机号密码登录成功");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.debug(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
			Logger.verifyEquals(token.get("deviceId").toString(), usernameandpwdloginBean.getDeviceId(), "验证deviceId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_008() {
		Logger.start(true,"商家手机号密码登录成功-没有微信信息");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setOpenId(null);
			u.setUnionId(null);
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.debug(token.toString());
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
			Logger.verifyEquals(token.get("deviceId").toString(), usernameandpwdloginBean.getDeviceId(), "验证deviceId");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_009() {
		Logger.start(true,"已删除商家手机号密码登录失败");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			Integer user = DGUserCallService.UserCreateCall(u);
			DGUserCallService.DeleteUser(user);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_010() {
		Logger.start(true,"独立商家账户，登录公版小程序，失败");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			
			
			usernameandpwdloginBean.setAppSource(u.getAppSource());
//			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_011() {
		Logger.start(true,"独立商家账户，登录自己小程序，成功");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			
			
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setWxAppName(user+"");
//			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_012() {
		Logger.start(true,"独立商家账户使用非手机号用户名登录，登录自己小程序，成功");
		try {
			CreateUserBackBean u = DGUserCallService.initUsercreateNewBean();
			u.setUserType(1);
			Integer user = DGUserCallService.UserCreateNewCall(u);
			DGUserCallService.ReceiveUserCreateNewCall(user);
			
			
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setWxAppName(user+"");
//			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginBean.setDeviceId(TestDataManager.getRandomUUID().toString());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_013() {
		Logger.start(true,"超级管理员登录成功");
		try {
			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
			u.setIsSetPwd(true);
			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(u.getAppSource());
			usernameandpwdloginBean.setClientIp(u.getClientIp());
			usernameandpwdloginBean.setPassword(u.getLoginPwd());
			usernameandpwdloginBean.setUsername(u.getMobile());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
			String sid1=usernameandpwdloginCall.getString("accessToken");
			DGUserWapper wapper=new DGUserWapper();
			Logger.verifyEquals(usernameandpwdloginBean.getUsername(), wapper.selectDGLoginLog(user).get(0).getUserName(), "验证Username");
			
			usernameandpwdloginBean.setPassword("33333");
			usernameandpwdloginBean.setUsername("...gmp..."+u.getMobile());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(user.toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(u.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
			Logger.verifyNotEquals(sid1, usernameandpwdloginCall.getString("accessToken"), "验证accessToken重新生成");
			Logger.verifyEquals(usernameandpwdloginBean.getUsername(), wapper.selectDGLoginLog(user).get(0).getUserName(), "验证Username");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_014() {
		Logger.start(true,"客服用户名密码登录成功");
		try {
			Integer mchId= GodmchManageCallService.CreateSellerMch();
			mchCreateCsBean mchCreateCsBean = GodmchManageCallService.initMchCreateCsBean(mchId);
			GodmchManageCallService.mchCreateCsCall(mchCreateCsBean);
			cs_info csi2=new DefGodMchWapper().selectCsInfoByMchId(mchCreateCsBean.getMchId()).get(0);
//			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
//			u.setIsSetPwd(true);
//			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usernameandpwdloginBean.setClientIp(TestDataManager.getRandomIp());
			usernameandpwdloginBean.setPassword(mchCreateCsBean.getPassword());
			usernameandpwdloginBean.setUsername(mchCreateCsBean.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("true", usernameandpwdloginCall.getString("success"), "验证success");
			Logger.verifyEquals(csi2.getCsId().toString(),usernameandpwdloginCall.getString("userId"), "验证uid");
			Map<String, Object> token = new UserTokenWapper().getUserTokenByUid(Integer.valueOf(usernameandpwdloginCall.getString("userId")));
			Logger.verifyEquals(AppSourceEnum.parse(usernameandpwdloginBean.getAppSource()).toString().toLowerCase(), token.get("tSource"), "验证tSource");
			Logger.verifyEquals(token.get("sId").toString(), usernameandpwdloginCall.getString("accessToken"), "验证accessToken");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Usernameandpwdlogin_015() {
		Logger.start(true,"template11邀请客服用户名密码登录-拦截");
		try {
			Integer mchId= GodmchManageCallService.CreateSellerMch();
			mchCreateCsBean mchCreateCsBean = GodmchManageCallService.initMchCreateCsBean(mchId);
			mchCreateCsBean.setTemplateId(11);
			GodmchManageCallService.mchCreateCsCall(mchCreateCsBean);
			cs_info csi2=new DefGodMchWapper().selectCsInfoByMchId(mchCreateCsBean.getMchId()).get(0);
//			UserCreateBean u = DGUserCallService.initSellerUsercreateBean();
//			u.setIsSetPwd(true);
//			Integer user = DGUserCallService.UserCreateCall(u);
			usernameandpwdloginBean.setAppSource(DGUserCallService.DG_SellerWxApp);
			usernameandpwdloginBean.setClientIp(TestDataManager.getRandomIp());
			usernameandpwdloginBean.setPassword(mchCreateCsBean.getPassword());
			usernameandpwdloginBean.setUsername(mchCreateCsBean.getLoginId());
			usernameandpwdloginCall.setData(usernameandpwdloginBean);
			usernameandpwdloginCall.callService();			
			Logger.verifyEquals("false", usernameandpwdloginCall.getString("success"), "验证success");
			//对不起，该账号暂无权限登录后台
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}